﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;



namespace DotNetNuke.Tests.UI.WatiN.RegressionTests
{
    [TestFixture]


    public class RedAdminBorderTests : RTBase
    {

        #region Test Setup and TearDown

        public bool LanguagePackInstalled = false;

        [SetUp]
        public void SetUp()
        {

        }

        [TearDown]
        public void TearDown()
        {
            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Verify_Hide_Admin_Border_Settings_In_Module_Settings()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);

            //Add a page and module
            RibbonBar ribbonBar = new RibbonBar(homePage);
            ribbonBar.NewPageLink.Click();

            PageSettingsPage page = new PageSettingsPage(homePage);
            page.AddPage("Test Page");

            ModulePage module = new ModulePage(homePage);
            module.GetModuleSettingsImage(0).Click();
            module.AdvancedSettingsExpandButton.Click();
            
            //Assert
            Assert.IsTrue(module.DisplayAdminBorderCheckbox.Exists);
        }

        [Test]
        public void Verify_Red_Admin_Border_Is_Hidden_On_A_Module()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);

            //Add a page and module
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            RibbonBar ribbonBar = new RibbonBar(homePage);
            ribbonBar.NewPageLink.Click();

            PageSettingsPage page = new PageSettingsPage(homePage);
            page.AddPage("Test Page");
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));

            ModulePage module = new ModulePage(homePage);
            module.GetModuleSettingsImage(0).Click();

            module.AdvancedSettingsExpandButton.Click();
            module.DisplayAdminBorderCheckbox.Checked = true;
            module.UpdateLink.Click();
            System.Threading.Thread.Sleep(2000);

            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));
        }

        [Test]
        public void Verify_Border_Behavior_If_Module_Expiry_Set_To_Past_Date()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);

            //Add a page and module
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            RibbonBar ribbonBar = new RibbonBar(homePage);
            ribbonBar.NewPageLink.Click();

            PageSettingsPage page = new PageSettingsPage(homePage);
            page.AddPage("Test Page");
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));

            ModulePage module = new ModulePage(homePage);
            module.GetModuleSettingsImage(0).Click();

            module.AdvancedSettingsExpandButton.Click();
            module.DisplayAdminBorderCheckbox.Checked = true;
            module.EndDateField.Value = "8/17/2009";
            module.UpdateLink.Click();
            System.Threading.Thread.Sleep(2000);

            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Module Expired - 8/17/2009"));
        }

        [Test]
        public void Verify_Border_Behavior_If_Module_Expiry_Set_To_Future_Date()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);
            
            //Add a page and module
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            RibbonBar ribbonBar = new RibbonBar(homePage);
            ribbonBar.NewPageLink.Click();

            PageSettingsPage page = new PageSettingsPage(homePage);
            page.AddPage("Test Page");
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));

            ModulePage module = new ModulePage(homePage);
            module.GetModuleSettingsImage(0).Click();

            module.AdvancedSettingsExpandButton.Click();
            module.DisplayAdminBorderCheckbox.Checked = true;
            module.EndDateField.Value = "8/17/2100";
            module.UpdateLink.Click();
            System.Threading.Thread.Sleep(2000);

            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));
            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Module Expired - 8/17/2100"));
        }

        #endregion

        #region Public Methods

        private void SelectRibbonBarAsControlPanelStyle(string hostEmail, WatiNBase homePage)
        {
            HostSettingsPage hostSettings = new HostSettingsPage(homePage);
            hostSettings.HostEmailTextField.Value = hostEmail;
            hostSettings.OtherSettingsExpandButton.Click();
            hostSettings.ControlPanelStyleSelectList.Select("RIBBONBAR");
            hostSettings.UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);
        }


        #endregion

    }
}